package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.InputPwdActivity;
import flc.ast.activity.LocalPlayActivity;
import flc.ast.activity.MovieActivity;
import flc.ast.activity.PlayUriActivity;
import flc.ast.activity.SelVideoActivity;
import flc.ast.activity.SetPwdActivity;
import flc.ast.adapter.MovieAdapter;
import flc.ast.bean.MyVideoBean;
import flc.ast.databinding.FragmentPlayBinding;
import flc.ast.utils.Content;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.utils.StkPermissionHelper;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PlayFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PlayFragment extends BaseNoModelFragment<FragmentPlayBinding> {

    private MovieAdapter movieAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_play;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivPlayUri.setOnClickListener(this);
        mDataBinding.ivVideoCompress.setOnClickListener(this);
        mDataBinding.ivLocalVideo.setOnClickListener(this);
        mDataBinding.tvMovieAll.setOnClickListener(this);
        mDataBinding.rlPrivacyVideo.setOnClickListener(this);

        mDataBinding.rvMovieList.setLayoutManager(new LinearLayoutManager(mContext));
        movieAdapter = new MovieAdapter();
        mDataBinding.rvMovieList.setAdapter(movieAdapter);
        movieAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPlayUri://链接博播放
                startActivity(PlayUriActivity.class);
                break;
            case R.id.ivVideoCompress://视频压缩
                goSelPermission(4);
                break;
            case R.id.ivLocalVideo://本地视频
                goLocalPermission();
                break;
            case R.id.tvMovieAll://影视解读
                startActivity(MovieActivity.class);
                break;
            case R.id.rlPrivacyVideo://加密视频
                //判断是否设置密码
                boolean isHave = SPUtil.getBoolean(mContext, Content.isHavePassword, false);
                if (isHave) {
                    startActivity(InputPwdActivity.class);
                } else {
                    startActivity(SetPwdActivity.class);
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        getData();
        getVideoData();
    }

    private void getData() {
        StkResApi.getTagResourceList(this, AppConfig.MOVIE_DATA, StkResApi.createParamMap(0, 3), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        movieAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getVideoData() {
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myPrivacy;
        List<File> files = FileUtils.listFilesInDir(path);
        long size = 0;
        if (files.size() > 0) {
            for (File file : files) {
                size += file.length();
            }
        }
        mDataBinding.tvPrivacyNum.setText(getString(R.string.video_num, files.size(), ConvertUtils.byte2FitMemorySize(size, 2)));
    }

    private void goSelPermission(int type) {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_storage_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        SelVideoActivity.type = type;
                        startActivity(SelVideoActivity.class);
                    }
                }).request();
    }

    private void goLocalPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_storage_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        startActivity(LocalPlayActivity.class);
                    }
                }).request();
    }

}